package com.newsoft.frame.codegen;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.cw.jeeyt.common.DateTool;
import com.cw.jeeyt.common.enums.EnumGenerateCodeType;
import com.cw.jeeyt.common.enums.EnumTemplateType;
import com.cw.jeeyt.common.utils.StringUtils;
import com.cw.jeeyt.service.bo.system.SystemTemplateModuleInfoBo;
import com.newsoft.frame.codegen.parameter.ClassField;
import com.newsoft.frame.codegen.parameter.ParamBundle;
import com.newsoft.frame.codegen.parameter.ParamContext;

/**
 * To generate the java source file.
 *
 * @author chenw
 */
public class XmlFileGenerator extends AbstractSourceFileGenerator {
    @SuppressWarnings("unchecked")
    @Override
    public void generateConcreteFiles(ParamContext paramContext) throws Exception {

        String outputParentDir = paramContext.getXmlOutputPath();
        String generateType = (String) paramContext.getAttributes()
            .get("generateType");
        List<SystemTemplateModuleInfoBo> stmibList = (List<SystemTemplateModuleInfoBo>) paramContext.getAttributes()
            .get("systemTemplateModuleInfoList");
        ParamBundle pramBundle = new ParamBundle();
        pramBundle.setGenerateType(generateType);
        //生成代码
        if (stmibList != null && !stmibList.isEmpty()) {
            for (SystemTemplateModuleInfoBo stmib : stmibList) {
                if (EnumTemplateType.XML.getCode()
                    .equals(stmib.getTemplateModuleType())) {
                    createSingleFile(outputParentDir, pramBundle, stmib.getTemplateModuleName());
                }
            }
        }
        logger.info("Finished to create java source at directory: " + outputParentDir);
    }

    private void createSingleFile(String outputParentDir, ParamBundle paramBundle, String templateName)
    throws Exception {
        String generateType = paramBundle.getGenerateType();

        Configuration configuration;
        if (EnumGenerateCodeType.PAGE.getCode()
            .equals(generateType)) {
            configuration = Configuration.getPageConfig();
        } else if (EnumGenerateCodeType.CLASS.getCode()
            .equals(generateType)) {
            configuration = Configuration.getClassConfig();
        } else if (EnumGenerateCodeType.JDBC.getCode()
            .equals(generateType)) {
            configuration = Configuration.getJdbcConfig();
        } else if (EnumGenerateCodeType.PROPERTIES.getCode()
            .equals(generateType)) {
            configuration = Configuration.getDefault();
        } else {
            throw new Exception("不能识别当前生成代码方式");
        }
        paramBundle.setModuleId(configuration.getModuleId());
        List<ClassField> classFieldList = new ArrayList<>();
        String entityClasses = configuration.getEntityClasses();
        String packageNames = configuration.getPackages();
        String[] packageNameArray = packageNames.split(",");
        String[] entityNamesArray = entityClasses.split(",");
        int count = packageNameArray.length;
        if (StringUtils.isNotEmpty(packageNames)) {
            if (packageNameArray != null && packageNameArray.length > 0) {
                paramBundle.setPackageName(packageNameArray[0]);
                for (int i = 0; i < count; i++) {
                    ClassField classField = new ClassField();
                    String moduleName = entityNamesArray[i].substring(0, 1)
                        .toLowerCase() + entityNamesArray[i].substring(1);
                    classField.setClassName(entityNamesArray[i]);
                    classField.setModuleName(moduleName);
                    String packageName = packageNameArray[i];
                    String packagePathName = packageName.replace(".", "/");
                    classField.setPackageName(packageName);
                    classField.setPackagePathName(packagePathName);
                    classFieldList.add(classField);
                }
            }
        }
        paramBundle.setModuleName("xxx");
        paramBundle.setClassFieldList(classFieldList);
        String template = configuration.getXmlTemplatePath() + "/" + templateName;
        File file = new File(template);
        if (!file.exists()) {
            return;
        }
        String outputPath = outputParentDir + "/" + Constant.XML_OUTPUT_PATH + "/" + templateName.replaceAll("xxx",
            configuration.getModuleId());
        File outputFile = new File(outputPath);
        if (outputFile.exists()) {
            Date date = new Date();
            String dateStr = DateTool.formatDate(date, "yyyyMMddhhmmss");
            outputPath = outputParentDir + "/" + Constant.XML_OUTPUT_PATH + "/" + dateStr + templateName.replaceAll(
                "xxx", configuration.getModuleId());
        }
        templateEngine.process(paramBundle, templateName, outputPath);
    }

}
